/**************************************************************************
 *         Copyright(c) 2007 by Sunplus mMedia  Technology Co., Ltd.       *
 *                                                                         *
 *  This software is copyrighted by and is the property of Sunplus mMedia  *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno- *
 *  logy Co., Ltd. This software may only be used in accordance with the   *
 *  corresponding license agreement. Any unauthorized use, duplication,    *
 *  distribution, or disclosure of this software is expressly forbidden.   *
 *                                                                         *
 *  This Copyright notice "M U S T" not be removed or modified without     *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.          *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this  *
 *  software without notice.                                               *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd.                                    *
 *  19-1, Innovation First Road, Science-Based Industrial Park,            *
 *  Hsin-Chu, Taiwan, R.O.C.                                               *
 *                                                                         *
 **************************************************************************/

#include "general.h"
#include "app_main.h"
#include "os_msg.h"
#include "initio.h"
#include "app_multi_play.h"
#include "app_still_play.h"
#include "app_ui_power.h"
#include "app_ui_para.h"
#include "app_menu_micro.h"
#include "sp1k_pb_api.h"
#include "sp1k_hal_api.h"
#include "sp1k_util_api.h"
#include "app_ui_osd.h"
#include "sp1k_osd_api.h"
#include "sp1k_aud_api.h"
#include "app_menu_tab.h"
#include "key_scan.h"
#include "smcfunc.h"
/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/

/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/
 extern SIMAGE xdata pbFile;

//-----------------------------------------------------------------------------
//appMultiPlay
//-----------------------------------------------------------------------------

void appMultiPlay (UINT8 msg)
{
	UINT8 InitMode;
//	powerBatLv_t batlv;
	
	switch (msg) {
		case SP1K_MSG_STATE_INIT:
			if (!sp1kPbFileCntGet()) {
				osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);
				break;
			}
			if (appPreStateGet(0) == APP_STATE_MENU) {
				InitMode = SP1K_PB_INIT_SMART;
			} else {
				InitMode = SP1K_PB_INIT_FULL;
			}
			appMultiPlayInit(InitMode);
			break;
	    	case SP1K_MSG_KEY_PRESS_S1:
			break;
	    	case SP1K_MSG_KEY_PRESS_S2:
			break;
	    	case SP1K_MSG_KEY_PRESS_MENU:
			osMsgPost(SP1K_MSG_STATE_TO_MENU);
			break;
	    	case SP1K_MSG_KEY_PRESS_UP:
			appMultiPlayShift(PB_SHIFT_UP);
			break;
	    	case SP1K_MSG_KEY_PRESS_DOWN:
			appMultiPlayShift(PB_SHIFT_DOWN);
			break;
	    	case SP1K_MSG_KEY_PRESS_LEFT:
			appMultiPlayShift(PB_SHIFT_LEFT);
			break;
	    	case SP1K_MSG_KEY_PRESS_RIGHT:
			appMultiPlayShift(PB_SHIFT_RIGHT);
			break;
	    	case SP1K_MSG_KEY_PRESS_OK:
			break;
	    	case SP1K_MSG_KEY_PRESS_TELE:
			osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);			
			break;
	    	case SP1K_MSG_KEY_PRESS_WIDE:
			break;
	    	case SP1K_MSG_KEY_PRESS_DISP:
			break;
	    	case SP1K_MSG_KEY_PRESS_F1:
			osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);
			break;
	    	case SP1K_MSG_KEY_PRESS_PB:
			//printf("PB\n");
			//suny add for pb mode -> other mode start
#if(TV_OPTION == TV_PLAY_OPTION) 
			if(appPanelTypeGet() == 0) {
				return;
			}
#endif
			switch (appPbPreStateGet())
			{
				case APP_STATE_STILL_VIEW:
					msg = SP1K_MSG_STATE_TO_STILL_VIEW;
					break;	
				case APP_STATE_VIDEO_VIEW:
					msg = SP1K_MSG_STATE_TO_VIDEO_VIEW;
					break;
		
				case APP_STATE_AUDIO_REC:
					sp1kAudioRecTypeSet(AUDIO_TYPE_NORMAL);
					msg = SP1K_MSG_STATE_TO_AUDIO_REC;
					break;
				default:
					msg = SP1K_MSG_STATE_TO_STILL_VIEW;
					break;
			}
#if(TV_OPTION == TV_PLAY_OPTION) 
				if(appPanelTypeGet() == 0) {
					osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);	
				} else {
					osMsgPost(msg);
				}
#else
				osMsgPost(msg);
#endif
			//suny add for pb mode -> other mode end
			break;
			/*jintao.liu 2008-3-28 add for mantis 22007*/
		case SP1K_MSG_CARD_PLUG_IN:
			appStorageMount(K_MEDIA_SDF);
			//pbFile.dwIndex = sp1kPbFileCntGet();
            pbFile.dwIndex = 0xffff; //xian ++, invalid index for #31928
			osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);
			break;
		case SP1K_MSG_CARD_PLUG_OUT:
			appStorageMount(K_MEDIA_NANDF);
			//pbFile.dwIndex = sp1kPbFileCntGet();
            pbFile.dwIndex = 0xffff; //xian ++, invalid index for #31928
			osMsgPost(SP1K_MSG_STATE_TO_STILL_PLAY);

			//add for mantis #36936@start
			if((appSDPlugStatusGet()==0) && (sp1kDiskTypeGet()==DEV_TYPE_DISK_SPI))
			{
				osMsgFlush();
				uiOsdDialogDisp(ID_STR_NO_CARD_, 0);
			}
			//add for mantis #36936@end
			
			break;
	    	case SP1K_MSG_USB_PLUG_IN:
			if (storageReadyflag==SUCCESS) {
				if (USB_DBG) {
					sp1kHalUSBSwPlugIn();
				} else {
					osMsgPost(SP1K_MSG_STATE_TO_USB);
				}				
			}
			break;
	    	case SP1K_MSG_USB_PLUG_OUT:
			break;
	    	case SP1K_MSG_TV_PLUG_IN:
	    	case SP1K_MSG_TV_PLUG_OUT:
			appDispSet();
			break;
		case SP1K_MSG_BAT_LV_UPDATE://jintao.liu 2008-3-13 add for battery action
			uiUpdateBatteryLevel();
			appBattStsHandle();
			break;
		case SP1K_MSG_BAT_EMPTY:
			sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
			uiUpdateOSDBatEmpty();
			osMsgPost(SP1K_MSG_POWER_OFF);
			sp1kBtnDisableAll();//ready to power off, disable all button!
			break;

		case SP1K_MSG_POWER_OFF:
		case SP1K_MSG_KEY_PRESS_POWER:
			sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
			appPowerOffProc();
			break;
		case SP1K_MSG_NAND_ERROR:
		case SP1K_MSG_CARD_ERROR:
			uiOsdDialogDisp(ID_STR_CARD_ERROR, 0);
			break;
	    	default:
			//printf("Unknown message:%d\n", (UINT16)msg);
			break;
	}
}

UINT8 appMultiPlayInit(UINT8 InitMode)
{
	//CPrintf2("idx %u, tot %u",  pbFile.dwIndex,  sp1kPbFileCntGet());
	if (InitMode == SP1K_PB_INIT_SMART) {
		sp1kPbMultiPlayObjFrameDraw(pbFile.dwIndex, 1);
		return SUCCESS;		
	}
	sp1kPbMultiPlayLayoutInit(PB_MULTIPLAY_LAYOUT_9, pbFile.dwIndex);
	sp1kPbMultiPlay();
	//appPbOsdInit();   //jintao.liu 2008-5-22 mantis 25063
	sp1kOsdClear(0, 0); //jintao.liu 2008-5-22 mantis 25063
	appUiCoverTVOsd();
	
	return SUCCESS;
}


UINT8 appMultiPlayShift(UINT8 shift)
{
	sp1kBtnDisableAll();
	sp1kPbMultiPlayShift(&pbFile.dwIndex, shift); 
	appPbFileInfoUpdate();
	sp1kBtnEnableAll();
	//appPbOsdInit();   ////jintao.liu 2008-5-22 mantis 25063
	
	return SUCCESS;	
}
